require 'dict'

function create_embs( )
	-- load dic --
	local dic = Dict:new()
	dic:load(dic_path, true)

	-- load org embs --
	f = torch.DiskFile(org_embs_path, 'r')
	local info = f:readInt(2)
	local org_nword = info[1]	
	local embdim = info[2]	
	local org_embs = torch.Tensor(f:readDouble(org_nword*embdim))
					:resize(org_nword, embdim)
	f:close()

	-- create new embs --
	-- first word is $START$
	-- second word is $END$
	local embs = torch.Tensor(dic:size(), embdim)
	embs[{{3,org_nword+2},{}}]:copy(org_embs)
	embs[{1,{}}]:copy(
					embs[{dic.word2id['UNKNOWN'],{}}] +
					torch.randn(embdim):mul(0.01))
	embs[{2,{}}]:copy(
					embs[{dic.word2id['UNKNOWN'],{}}] +
					torch.randn(embdim):mul(0.01))

	-- create embs for words not in the org dic
	for i = org_nword+3, dic:size() do
		embs[{i,{}}]:copy(
					embs[{dic.word2id['UNKNOWN'],{}}] +
					torch.randn(embdim):mul(0.01))
	end

	return dic, embs
end

if #arg ~= 3 then
	print(
		"invalid arguments: " ..
		" <org_embs_path> <dic_path> <output_path>" )
		
else
	org_embs_path = arg[1]
	dic_path = arg[2]
	output_path = arg[3]

	dic, embs = create_embs()
	f = torch.DiskFile(output_path, 'w')
	f:writeObject(dic)
	f:writeObject(embs)
	f:close()
end

